package arbor.mining.util.stock;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import arbor.mining.util.date.DateUtil;
import arbor.mining.util.net.WebStringConn;

public class PCHomeGather extends StockDataGather {

	public PCHomeGather() {
		super();
	}

	public StockEntity[] getInstantWTXPrice() {
		StockEntity wtx1Entity = null;
		StockEntity wtx0Entity = null;
		WebStringConn conn = new WebStringConn(null);
		logger.info("Get " + urlProp.getProperty("TODAY_INSTANT_INDEX_URL"));
		String dayc = conn.getAllTableContent((urlProp
				.getProperty("TODAY_INSTANT_INDEX_URL")), false,false);
		//System.out.println(dayc);
		StockEntity[] wtx = parseInstantPrice(dayc);
		return wtx;
	}

	private StockEntity[] parseInstantPrice(String dayc) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat hdf = new SimpleDateFormat("hh:mm:ss");
		StockEntity[] entities = new StockEntity[3];

		StringTokenizer token = new StringTokenizer(dayc, "\n");
		int isToday = -1;
		String line = token.nextToken();

		while (isToday == -1) {
			if (line.startsWith("資料日期：")) {
				String day = line.split("： ")[1];
				if (DateUtil.getCurrentDashedDate().equals(day)) {
					isToday = 1;
				} else {
					logger.warn("Not today's data!!\n");
					isToday = 0;
				}
			} else
				line = token.nextToken();
		}

		if (isToday == 1) {
			int wtx0Found = -1;
			int wtx1Found = -1;
			int wtx2Found = -1;
			while (wtx0Found == -1) {
				if (!line.equals("台現貨")) {
					line = token.nextToken();
				} else {
					String time = token.nextToken();
					Date min;
					try {
						min = df.parse(time);
					} catch (ParseException e) {
						logger.warn("No time info");
						min = null;
					}
					if (min != null) {
						GregorianCalendar startTime = new GregorianCalendar();
						GregorianCalendar endTime = new GregorianCalendar();

						startTime.setTime(min);
						startTime.set(GregorianCalendar.getInstance().get(
								Calendar.YEAR), GregorianCalendar.getInstance()
								.get(Calendar.MONTH), GregorianCalendar
								.getInstance().get(Calendar.DATE));
						endTime.setTime(min);
						endTime.set(GregorianCalendar.getInstance().get(
								Calendar.YEAR), GregorianCalendar.getInstance()
								.get(Calendar.MONTH), GregorianCalendar
								.getInstance().get(Calendar.DATE));

						endTime.set(Calendar.HOUR_OF_DAY, 13);
						endTime.add(Calendar.MINUTE, 1);
						entities[0] = new StockEntity(
								StockEntity.RANGE_TYPE_DAY, startTime, endTime);
					} else {
						Date todayday = DateUtil.getCurrentDate();
						GregorianCalendar startTime = new GregorianCalendar();
						GregorianCalendar endTime = new GregorianCalendar();

						startTime.setTime(todayday);
						startTime.set(Calendar.HOUR_OF_DAY, 9);
						startTime.set(Calendar.MINUTE, 0);
						startTime.set(Calendar.SECOND, 0);
						endTime.setTime(todayday);
						endTime.set(Calendar.HOUR_OF_DAY, 13);
						endTime.set(Calendar.MINUTE, 30);
						endTime.set(Calendar.SECOND, 0);
						entities[0] = new StockEntity(
								StockEntity.RANGE_TYPE_DAY, startTime, endTime);
					}

					/* 成交價 */
					token.nextToken();
					token.nextToken();
					entities[0].ohlc.close = Double.parseDouble(token
							.nextToken());
					token.nextToken();
					token.nextToken();
					token.nextToken();
					/* 開盤價 最高 最低 */
					entities[0].ohlc.open = Double.parseDouble(token
							.nextToken());
					entities[0].ohlc.high = Double.parseDouble(token
							.nextToken());
					entities[0].ohlc.low = Double.parseDouble(token.nextToken());

					wtx0Found = 1;
				}

			}

			while (wtx1Found == -1) {
				if (!line.startsWith("台指")) {
					line = token.nextToken();
				} else {
					String time = token.nextToken();
					Date min;
					try {
						min = hdf.parse(time);
					} catch (ParseException e) {
						logger.warn("No time info");
						min = null;
					}
					if (min != null) {
						GregorianCalendar startTime = new GregorianCalendar();
						GregorianCalendar endTime = new GregorianCalendar();

						startTime.setTime(min);
						startTime.set(GregorianCalendar.getInstance().get(
								Calendar.YEAR), GregorianCalendar.getInstance()
								.get(Calendar.MONTH), GregorianCalendar
								.getInstance().get(Calendar.DATE));

						startTime.set(Calendar.HOUR_OF_DAY, 8);
						startTime.set(Calendar.MINUTE, 45);
						startTime.set(Calendar.SECOND, 0);
						
						endTime.setTime(min);
						endTime.set(GregorianCalendar.getInstance().get(
								Calendar.YEAR), GregorianCalendar.getInstance()
								.get(Calendar.MONTH), GregorianCalendar
								.getInstance().get(Calendar.DATE));
	
						entities[1] = new StockEntity(
								StockEntity.RANGE_TYPE_DAY, startTime, endTime);
					} else {
						logger.error("Time parse error!!\n");
						line = token.nextToken();
						continue;
					}

					/* 成交價 */
					token.nextToken();
					token.nextToken();
					entities[1].ohlc.close = Double.parseDouble(token
							.nextToken());
					/* 成交量 */
					entities[1].volume = Integer.parseInt((token.nextToken()
							));
					token.nextToken();
					token.nextToken();
					/* 開盤價 最高 最低 */
					entities[1].ohlc.open = Double.parseDouble(token
							.nextToken());
					entities[1].ohlc.high = Double.parseDouble(token
							.nextToken());
					entities[1].ohlc.low = Double.parseDouble(token.nextToken()
							);

					wtx1Found = 1;
				}

			}

			/*下月*/
			line = token.nextToken();
			while (wtx2Found == -1) {
				if (!line.startsWith("台指")) {
					line = token.nextToken();
				} else {
					String time = token.nextToken();
					Date min;
					try {
						min = hdf.parse(time);
					} catch (ParseException e) {
						logger.warn("No time info");
						min = null;
					}
					if (min != null) {
						GregorianCalendar startTime = new GregorianCalendar();
						GregorianCalendar endTime = new GregorianCalendar();

						startTime.setTime(min);
						startTime.set(GregorianCalendar.getInstance().get(
								Calendar.YEAR), GregorianCalendar.getInstance()
								.get(Calendar.MONTH), GregorianCalendar
								.getInstance().get(Calendar.DATE));

						startTime.set(Calendar.HOUR_OF_DAY, 8);
						startTime.set(Calendar.MINUTE, 45);
						startTime.set(Calendar.SECOND, 0);
						
						endTime.setTime(min);
						endTime.set(GregorianCalendar.getInstance().get(
								Calendar.YEAR), GregorianCalendar.getInstance()
								.get(Calendar.MONTH), GregorianCalendar
								.getInstance().get(Calendar.DATE));
	
						entities[2] = new StockEntity(
								StockEntity.RANGE_TYPE_DAY, startTime, endTime);
					} else {
						logger.error("Time parse error!!\n");
					}

					/* 成交價 */
					token.nextToken();
					token.nextToken();
					entities[2].ohlc.close = Double.parseDouble(token
							.nextToken());
					/* 成交量 */
					entities[2].volume = Integer.parseInt((token.nextToken()
							));
					token.nextToken();
					token.nextToken();
					/* 開盤價 最高 最低 */
					entities[2].ohlc.open = Double.parseDouble(token
							.nextToken());
					entities[2].ohlc.high = Double.parseDouble(token
							.nextToken());
					entities[2].ohlc.low = Double.parseDouble(token.nextToken()
							);

					wtx2Found = 1;
				}

			}
		}
		return entities;
	}

	public static void main(String[] args) {
		StockEntity[] entities = new PCHomeGather().getInstantWTXPrice();
		System.out.println(entities[0]);
		System.out.println(entities[1]);
	}
}
